package data_report

import (
	"context"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/analysisClient/rpc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type IpReportLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 相同ip报表
func NewIpReportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *IpReportLogic {
	return &IpReportLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *IpReportLogic) IpReport(req *types.IpReportRequest) (resp types.IpReportResultResponse, err error) {
	resp = types.IpReportResultResponse{
		Data:     make([]types.IpReportResultData, 0),
		Page:     req.Page,
		PageSize: req.PageSize,
		Total:    0,
	}

	reqq := &rpc.SearchArSameipAnalysisReq{
		Page:      req.Page,
		Limit:     req.PageSize,
		CreatedAt: req.StartTime,
		Ip:        req.Ip,
		UserId:    req.UserId,
		EndAt:     req.EndTime,
	}

	analysis, err := l.svcCtx.Analysis.SearchArSameipAnalysis(l.ctx, reqq)
	if err != nil {
		return resp, err
	}

	for _, v := range analysis.ArSameipAnalysis {
		t := types.IpReportResultData{
			Date:    strconv.FormatInt(v.CreatedAt, 10),
			Ip:      v.Ip,
			Persons: v.IpCount,
		}
		resp.Data = append(resp.Data, t)
	}
	resp.Total = analysis.Total
	return
}
